package se.webbzon.oschi01.collision3d;

import java.util.ArrayList;
import java.util.Collection;

import javax.vecmath.Point3d;

public class CollisionCube3D extends CollisionModel3D {

	/*============================================================
	Constructors
	============================================================*/
	
	/** Creates a new collision cube with its center in the origin
	 * and with a width, height and depth equal to 1. **/
	public CollisionCube3D() {
		this(1,1,1);
	}
	
	/** Creates a new collision cube with its center in the origin
	 * and with a given width, height and depth. **/
	public CollisionCube3D(double width, double height, double depth) {
		super(createModel(width,height,depth));
	}
	
	/*============================================================
	Private Methods
	============================================================*/
	
	/** Creates the collision model. **/
	private static Collection<CollisionTriangle3D> createModel(	double width, 
																double height, 
																double depth) {
		final double w = width/2, h = height/2, d = depth/2;
		final ArrayList<CollisionTriangle3D> vertices = new ArrayList<CollisionTriangle3D>();
		
		// Create the corners the of the cube
		final Point3d tlf = new Point3d(-w,h,d);
		final Point3d trf = new Point3d(w,h,d);
		final Point3d tlb = new Point3d(-w,h,-d);
		final Point3d trb = new Point3d(w,h,-d);
		final Point3d blf = new Point3d(-w,-h,d);
		final Point3d brf = new Point3d(w,-h,d);
		final Point3d blb = new Point3d(-w,-h,-d);
		final Point3d brb = new Point3d(w,-h,-d);
		
		// Create top and bottom
		vertices.add(new CollisionTriangle3D(tlf,tlb,trf));
		vertices.add(new CollisionTriangle3D(trf,tlb,trb));
		vertices.add(new CollisionTriangle3D(blf,blb,brf));
		vertices.add(new CollisionTriangle3D(brf,blb,brb));
		
		// Create left and right side
		vertices.add(new CollisionTriangle3D(tlf,blf,blb));
		vertices.add(new CollisionTriangle3D(tlf,tlb,blb));
		vertices.add(new CollisionTriangle3D(trf,brf,brb));
		vertices.add(new CollisionTriangle3D(trf,trb,brb));
		
		// Create front and back
		vertices.add(new CollisionTriangle3D(tlf,blf,brf));
		vertices.add(new CollisionTriangle3D(tlf,trf,brf));
		vertices.add(new CollisionTriangle3D(tlb,blb,brb));
		vertices.add(new CollisionTriangle3D(tlb,trb,brb));
		
		return vertices;
	}

}
